package com.bd;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PetsModule {
       
    public final static String TABLE_NAME = "PetsModule";
    public final static String COL_PK = "id";
    public final static String COL_NAME = "name";
    public final static String COL_AGE = "age";
    public final static String COL_PICTURE = "picture";
    public final static String COL_SPECIES = "spicies";
    public final static String COL_RACE = "race";
    public final static String COL_HEIGHT = "height";
    public final static String COL_WEIGHT = "weight";
    public final static String COL_FOOD = "food";
    public final static String COL_VITAMINS = "vitamins";
        
    public final static String[] ALL_COLS =  {
    	PetsModule.COL_PK, 
    	PetsModule.COL_NAME, 
    	PetsModule.COL_AGE, 
    	PetsModule.COL_PICTURE,
    	PetsModule.COL_SPECIES,
    	PetsModule.COL_RACE,
    	PetsModule.COL_HEIGHT,
    	PetsModule.COL_WEIGHT,
    	PetsModule.COL_FOOD,
    	PetsModule.COL_VITAMINS,};
    
    private int pk = -1;
    private String name = "";
    private String age = "";
    private String picture = "";
    private String spicies = "";
	private String race = "";
	private String height = "";
	private String weight = "";
    private String food = "";
    private String vitamins = "";
       
       
    public PetsModule(String name, String age, String picture, String spicies, String race, String height, String weight, String food, String vitamins){
           
        this.name = name;
        this.age = age;
        this.picture = picture;
        this.spicies = spicies;
        this.race = race;
        this.height = height;
        this.weight = weight;
        this.food = food;
        this.vitamins = vitamins;
                
    }
    
    
       
    private PetsModule(){}
       
       

    @Override
    public String toString() {
        return "Pet [pk=" + this.pk + ", name=" + this.name + ", age=" + this.age + ", pic=" + this.picture + 
        		", species=" + this.spicies+ ", race=" + this.race+ ", height=" + this.height+ ", weight=" + this.weight + 
        		", food=" + this.food +  ", vitamins="+this.vitamins+ "]";
    }
       
    /***************************************************************************
     * SETTERS AND GETTERS
     **************************************************************************/
    
    public String getSpicies() {
		return spicies;
	}

	public void setSpicies(String spicies) {
		this.spicies = spicies;
	}

	public String getRace() {
		return race;
	}

	public void setRace(String race) {
		this.race = race;
	}

	public String getFood() {
		return food;
	}

	public void setFood(String food) {
		this.food = food;
	}
	
	public String getVitamins() {
		return vitamins;
	}

	public void setVitamins(String vitamins) {
		this.vitamins = vitamins;
	}

	
	
	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public int getPk() {
        return pk;
    }

    public void setPk(int pk) {
        this.pk = pk;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public String getWeight() {
        return weight;
        
    }
    
    public void setWeight(String weight) {
        this.weight = weight;
    }
    
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    /***************************************************************************
     * createTableString
     **************************************************************************/
    public static String createTableString(){
        String create = "CREATE TABLE " + PetsModule.TABLE_NAME + "(";
       
        create += PetsModule.COL_PK + " INTEGER PRIMARY KEY, ";
        create += PetsModule.COL_NAME + " STRING, ";
        create += PetsModule.COL_AGE + " INTEGER, ";
        create += PetsModule.COL_PICTURE + " STRING, ";
        create += PetsModule.COL_SPECIES + " STRING, ";
        create += PetsModule.COL_RACE + " STRING, ";
        create += PetsModule.COL_HEIGHT + " STRING, ";
        create += PetsModule.COL_WEIGHT + " STRING, ";
        create += PetsModule.COL_FOOD + " STRING, ";
        create += PetsModule.COL_VITAMINS + " STRING ";
        
   
        create += ");";
       
        return create;
    }
   
   
    /***************************************************************************
     * insertModule
     **************************************************************************/
    public PetsModule insertPet(SQLiteDatabase database){
       
        ContentValues values = new ContentValues();
        values.put(PetsModule.COL_NAME,this.getName());
	    values.put(PetsModule.COL_AGE,this.getAge());
	    values.put(PetsModule.COL_PICTURE,this.getPicture());
	    values.put(PetsModule.COL_SPECIES,this.getSpicies());
	    values.put(PetsModule.COL_RACE,this.getRace());
	    values.put(PetsModule.COL_HEIGHT,this.getHeight());
	    values.put(PetsModule.COL_WEIGHT,this.getWeight());
	    values.put(PetsModule.COL_FOOD,this.getFood());
	    values.put(PetsModule.COL_VITAMINS,this.getVitamins());
	   
	   
	    long insertId = database.insert(PetsModule.TABLE_NAME, null, values);
	   
	    return PetsModule.getPet(database, insertId);
    }
       
    /***************************************************************************
     * updateModule
     **************************************************************************/
    public boolean updatePet(SQLiteDatabase database){
       
        ContentValues values = new ContentValues();
        values.put(PetsModule.COL_NAME,this.getName());
	    values.put(PetsModule.COL_AGE,this.getAge());
	    values.put(PetsModule.COL_PICTURE,this.getPicture());
	    values.put(PetsModule.COL_SPECIES,this.getSpicies());
	    values.put(PetsModule.COL_RACE,this.getRace());
	    values.put(PetsModule.COL_HEIGHT,this.getHeight());
	    values.put(PetsModule.COL_WEIGHT,this.getWeight());
	    values.put(PetsModule.COL_FOOD,this.getFood());
	    values.put(PetsModule.COL_VITAMINS,this.getVitamins());
	   
       
        String whereClause = PetsModule.COL_NAME + "= \"" + this.name + "\"";
       
        int affected = database.update(PetsModule.TABLE_NAME, values,
                whereClause, null);
       
        if(affected==0){
            return false;
        }
        return true;
    }
       
    /***************************************************************************
     * getModule
     **************************************************************************/
    public static PetsModule getPet(SQLiteDatabase database, long insertId) {
       
        String selection = PetsModule.COL_PK + "=" + insertId;
       
        Cursor cursor = database.query(PetsModule.TABLE_NAME, PetsModule.ALL_COLS,
                        selection, null, null, null, null);
       
        cursor.moveToFirst();
        PetsModule module = PetsModule.cursorToPet(cursor);
        cursor.close();
        return module;
    }
       
    public static PetsModule getPet(SQLiteDatabase database, String id) {
       
        String selection = PetsModule.COL_PK + " = \"" + id + "\"";
       
        Cursor cursor = database.query(PetsModule.TABLE_NAME, PetsModule.ALL_COLS,
                selection, null, null, null, null);
       
        cursor.moveToFirst();
        PetsModule module = PetsModule.cursorToPet(cursor);
        cursor.close();
        return module;
    }
    /***************************************************************************
     * deleteModule
     **************************************************************************/
    public void deletePet(SQLiteDatabase database){
       
        //delete from the Module table
        database.delete(PetsModule.TABLE_NAME, PetsModule.COL_NAME + "=" + this.name, null);
        System.out.println("Module deleted: " + this.toString());
    }
       
    /***************************************************************************
     * cursorToModule
     **************************************************************************/
    private static PetsModule cursorToPet(Cursor cursor) {
       
        PetsModule Module = new PetsModule();
        Module.setPk(cursor.getInt(0));
        Module.setName(cursor.getString(1));
        Module.setAge(cursor.getString(2));
        Module.setPicture(cursor.getString(3));
        Module.setSpicies(cursor.getString(4));
        Module.setRace(cursor.getString(5));
        Module.setHeight(cursor.getString(6));
        Module.setWeight(cursor.getString(7));
        Module.setFood(cursor.getString(8));
        Module.setVitamins(cursor.getString(9));
        
        return Module;
    }
       
        /***************************************************************************
     * getAllModules
     **************************************************************************/
        public static List<PetsModule> getAllPets(SQLiteDatabase database) {
        List<PetsModule> modules = new ArrayList<PetsModule>();
       
        Cursor cursor = database.query(PetsModule.TABLE_NAME, PetsModule.ALL_COLS, null,
                null, null, null, null);
       
        if(cursor.getCount()==0){
            cursor.close();
            return modules;
        }
       
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            PetsModule module = PetsModule.cursorToPet(cursor);
            modules.add(module);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return modules;
    }
}
